<?php
/**
 * cms文章
 *
 *
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */

defined('ByShopWWI') or exit('Access Invalid!');
class articleControl extends CMSHomeControl{

    public function __construct() {

        parent::__construct();
        Tpl::output('index_sign', 'article');
    }

    public function indexOp() {
        $this->article_listOp();
    }

    /**
     * 文章列表
     */
    public function article_listOp() {
        $this->article();
	Tpl::output('index_sign', 'article');
        $model_article = Model('cms_article');

        //获取文章列表

        $page_number = 10;
        $template_name = 'article_list';



        $condition = array();

        //根据分类ID判断调用模板

        if(!empty($_GET['class_id'])) {
            if($_GET['class_id'] == "1"){

            }
            if($_GET['class_id'] == "2"){
                $template_name = 'article_list_question'; //调用有问有答模板
                Tpl::output('class_id', "2");
            }
            if($_GET['class_id'] == "3"){

            }
        }


        if($_GET['news']=='notice' | empty($_GET['news']) ){
            $para='notice';
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
                Tpl::output('notice', 'on');
        }

        if($_GET['news']=='media'){
            $para='media';
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('media', 'on');
        }

        if($_GET['news']=='question'){
            $para='question';
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('question', 'on');
        }

        $condition['article_state'] = self::ARTICLE_STATE_PUBLISHED;
        $article_list = $model_article->getList($condition, $page_number, 'article_sort asc, article_id desc');
        Tpl::output('show_page', $model_article->showpage(2));
        Tpl::output('article_li', $article_list);
        Tpl::output('class_id', 1);
        $this->get_article_sidebar();
        Tpl::showpage($template_name);
    }

    /**
     * 文章列表
     */
    public function article_detailOp() {
    	Tpl::output('index_sign', 'article');

    	$para=array('notice','media');
        $talk_id= $this->article_id($para);

        if(isset($_GET['article_id'])){
            $article_id = intval($_GET['article_id']);
            if(!in_array($article_id, $talk_id)) showMessage(Language::get('article_not_exist'),  CMS_SITE_MEDIA, '', 'error');

            if($article_id <= 0) {
                showMessage(Language::get('wrong_argument'),'','','error');
            }
        }else{
            $article_id= $talk_id[0];
        }



        if($article_id <= 0) {
            showMessage(Language::get('wrong_argument'),'','','error');
        }

        $model_article = Model('cms_article');
        $article_class=  Model('cms_article_class');

        //购买咨询
        $notice_class_id= $model_article->article_select('notice');
        //媒体报道
        $media_class_id= $model_article->article_select('media');

        $article_detail = $model_article->getOne(array('article_id'=>$article_id));

        $article_detail['article_class_id'];
        $article_title=$article_class->getList(array('class_id'=>$article_detail['article_class_id']), $page=null,
            $order='',$field='class_name');

        Tpl::output('class_name',$article_title[0]['class_name']);

        $class_class=$model_article->class_name($article_title[0]['class_name']);

        Tpl::output('class',$class_class);

        if(empty($article_detail)) {
            showMessage(Language::get('article_not_exist'), CMS_SITE_URL, '', 'error');
        }

        //相关文章
        $article_link_list = $this->get_article_link_list($article_detail['article_link']);
        Tpl::output('article_link_list', $article_link_list);

        //相关商品
        $article_goods_list = unserialize($article_detail['article_goods']);
        Tpl::output('article_goods_list', $article_goods_list);

        //计数加1
        $model_article->modify(array('article_click'=>array('exp','article_click+1')),array('article_id'=>$article_id));

        //文章心情
        $article_attitude_list = array();
        $article_attitude_list[1] = Language::get('attitude1');
        $article_attitude_list[2] = Language::get('attitude2');
        $article_attitude_list[3] = Language::get('attitude3');
        $article_attitude_list[4] = Language::get('attitude4');
        $article_attitude_list[5] = Language::get('attitude5');
        $article_attitude_list[6] = Language::get('attitude6');
        Tpl::output('article_attitude_list', $article_attitude_list);

        $condition=array();
        $condition['article_class_id'] = array('in',"$notice_class_id, $media_class_id");
        $ar_id_next= $model_article->articel_list($article_id, 'next',$condition);
        $ar_id_pre= $model_article->articel_list($article_id, 'pre',$condition);
        $bar_list=$model_article->bar_article(10,$condition);
        Tpl::output('id_next', $ar_id_next);
        Tpl::output('id_pre', $ar_id_pre);
        Tpl::output('bar_list', $bar_list);

        //分享
        $this->get_share_app_list();

        Tpl::output('article_detail', $article_detail);
        Tpl::output('detail_object_id', $article_id);
        $this->get_article_sidebar();

        //seo
        Tpl::output('seo_title', $article_detail['article_title']);

        Tpl::showpage('article_detail');
    }

    /**
     * 文章评论
     */
    public function article_comment_detailOp() {
        $article_id = intval($_GET['article_id']);
        if($article_id <= 0) {
            showMessage(Language::get('wrong_argument'),'','','error');
        }

        $model_article = Model('cms_article');
        $article_detail = $model_article->getOne(array('article_id'=>$article_id));
        if(empty($article_detail)) {
            showMessage(Language::get('article_not_exist'), CMS_SITE_URL, '', 'error');
        }

        $article_hot_comment = $model_article->getList(array('article_state'=>self::ARTICLE_STATE_PUBLISHED), null, 'article_comment_count desc', '*', 10);
        Tpl::output('hot_comment', $article_hot_comment);

        Tpl::output('article_detail', $article_detail);
        Tpl::output('detail_object_id', $article_id);
        Tpl::output('comment_all', 'all');

        //推荐文章
        $this->get_article_comment();

        Tpl::showpage('comment_detail');
    }


    /**
     * 文章列表
     */
    public function article_searchOp() {
        $condition = array();
        $condition['article_title'] = array("like",'%'.trim($_GET['keyword']).'%');
        $condition['article_state'] = self::ARTICLE_STATE_PUBLISHED;
        $model_article = Model('cms_article');
        $article_list = $model_article->getList($condition, 20, 'article_sort asc, article_id desc');
        Tpl::output('show_page', $model_article->showpage(2));
        Tpl::output('total_num', $model_article->gettotalnum());
        Tpl::output('article_list', $article_list);
        $this->get_article_sidebar();

        Tpl::showpage('search_article');
    }

    /**
     * 根据标签搜索
     */
    public function article_tag_searchOp() {
        $article_list = array();
        if(intval($_GET['tag_id']) > 0) {
            $model_article = Model('cms_article');

            $condition = array();
            $condition['relation_tag_id'] = $_GET['tag_id'];
            $condition['article_state'] = self::ARTICLE_STATE_PUBLISHED;
            $article_list = $model_article->getListByTagID($condition, 20, 'article_sort asc, article_id desc');

            Tpl::output('show_page', $model_article->showpage(2));
            Tpl::output('total_num', $model_article->gettotalnum());
        }

        Tpl::output('article_list', $article_list);
        $this->get_article_sidebar();

        Tpl::showpage('search_article');
    }

    /**
     * 文章侧栏
     */
    private function get_article_sidebar() {

        $model_tag = Model('cms_tag');
        $model_article = Model('cms_article');

        //标签
        $cms_tag_list = $model_tag->getList(TRUE, null, 'tag_sort asc', '', 10);
        $cms_tag_list = array_under_reset($cms_tag_list, 'tag_id');
        Tpl::output('cms_tag_list', $cms_tag_list);

        //推荐文章(图文)
        $condition = array();
        $condition['article_commend_image_flag'] = 1;
        $article_commend_image_list = $model_article->getList($condition, null, 'article_id desc', '*', 3);
        Tpl::output('article_commend_image_list', $article_commend_image_list);

        //推荐文章
        $this->get_article_comment();

    }

    private function article_id($para){
        $model_article = Model('cms_article');
        $condition_array= array();
        $article_id= array();
        $article_ids= array();

        foreach ($para as $value){
            $class_id=$model_article->article_select($value);
            array_push($article_ids, $class_id);
            $class_id= null;
        }

        $condition_array['article_class_id']= array('in', $article_ids);
        $para_id= $model_article->getList($condition_array, $page=null, $order='', $field='article_id', $limit='');

        foreach ($para_id as $value){
            array_push($article_id, $value['article_id']);
        }
        return $article_id;
    }

}
